package org.openkoala.koala.deploy.tree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Shell;
import org.openkoala.koala.deploy.action.EJBDeployAction;
import org.openkoala.koala.deploy.ejb.pojo.EJBDeploy;
import org.openkoala.koala.deploy.ejb.pojo.ImplObj;
import org.openkoala.koala.deploy.ejb.pojo.LocalInterface;
import org.openkoala.koala.deploy.ejb.pojo.RemoteInterface;
import org.openkoala.koala.deploy.pojo.InterfaceTree;
import org.openkoala.koala.deploy.pojo.Itree;
import org.openkoala.koala.deploy.pojo.LocalRemoteTree;
import org.openkoala.koala.deploy.pojo.MethodTree;

public class EJBSelectionUI extends SelectionUI<EJBDeploy> {

	private EJBDeployAction action;
	
	private EJBDeploy ejbDeploy;
	
	private  Button selfChecked;
	
	public EJBSelectionUI(Shell shell,EJBDeployAction action) {
		super(shell,"发布EJB服务");
		this.action = action;
		drawUI();
	}

    public void drawUI(){
        selfChecked = new Button(shell,SWT.CHECK);
        selfChecked.setText("是否支持本地服务发布");
        FormData data = new FormData();
        data.top = new FormAttachment(treeViewer.getTree(),20);
        data.left = new FormAttachment(treeViewer.getTree(),0,SWT.LEFT);
        selfChecked.setLayoutData(data);
        
        selfChecked.addSelectionListener(new SelectionListener(){
            
            @Override
            public void widgetSelected(SelectionEvent e) {
                boolean selected = selfChecked.getSelection();
                if(selected)treeViewer.getTree().setEnabled(false);
                else treeViewer.getTree().setEnabled(true);
            }
            
            @Override
            public void widgetDefaultSelected(SelectionEvent e) {
                
            }
            
        });
    }

	@Override
	public void initData(EJBDeploy ejbDeploy) {
		this.ejbDeploy = ejbDeploy;
		java.util.List<Itree> trees = new ArrayList<Itree>();
		Itree local = LocalRemoteTree.createLocalTreeNode();
		Itree remote = LocalRemoteTree.createRemoteTreeNode();
		trees.add(local);
		trees.add(remote);
		copyDeploy(local,ejbDeploy);
		copyDeploy(remote,ejbDeploy);
		treeViewer.setInput(trees);
		treeViewer.expandToLevel(2);
		treeViewer.refresh();
	}

	@Override
	public void process() {
		Map<String,List<String>> localMap = new HashMap<String,List<String>>();
		
		Map<String,List<String>> remoteMap = new HashMap<String,List<String>>();
		
		List<Itree> trees = (List)this.treeViewer.getInput();
		if(this.treeViewer.getTree().isEnabled()){
		for(Itree tree:trees){
			if(tree.isCheck()){
			List<InterfaceTree> interfaces = (List<InterfaceTree>)tree.getChildren();
			for(InterfaceTree interTree:interfaces){
				if(interTree.isCheck()){
					if(tree.getName().equals("local"))localMap.put(interTree.getName(), interTree.getSelectedChildren());
					if("remote".equals(tree.getName()))remoteMap.put(interTree.getName(), interTree.getSelectedChildren());
				}
			}
			}
		}
		for(ImplObj impl:this.ejbDeploy.getImpls()){
			String name = impl.getName();
			
			LocalInterface local = new LocalInterface();
			if(localMap.containsKey(name)){
				local.setMethods(localMap.get(name));
			}
			impl.setLocalInterface(local);
			RemoteInterface remote = new RemoteInterface();
			if(remoteMap.containsKey(name)){
				remote.setMethods(remoteMap.get(name));
			}
			impl.setRemoteInterface(remote);
		}
		}else{
			if(ejbDeploy.getImpls()!=null){
		    for(ImplObj impl:ejbDeploy.getImpls()){
		        impl.setLocalInterface(null);
		        impl.setRemoteInterface(null);
		    }
			}
		}
		ejbDeploy.setLocal(selfChecked.getSelection());
		action.setEjbDeploy(ejbDeploy);
        super.close();
		action.process();
	}
	
	private void copyDeploy(Itree tree, EJBDeploy ejbDeploy) {
		List<ImplObj> impls = ejbDeploy.getImpls();
	    if(impls==null){
	    	return;
	    }
		for (ImplObj impl : impls) {
			InterfaceTree interfaceTree = new InterfaceTree(impl.getName());
			for (String method : impl.getMethods()) {
				MethodTree methodTree = new MethodTree(method);
				interfaceTree.getChildren().add(methodTree);
			}
			tree.getChildren().add(interfaceTree);
		}
	}
	
}
